package com.zx.template.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.template.base.BeanCopyUtils;
import com.zx.template.base.PageBean;
import com.zx.template.base.R;
import com.zx.template.entity.VipConfigDO;
import com.zx.template.enums.VipConfigEnum;
import com.zx.template.mapper.VipConfigMapper;
import com.zx.template.pojo.dto.VipConfigDto;
import com.zx.template.pojo.vo.VipConfigVO;
import com.zx.template.service.VipConfigService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author D5
* @description 针对表【t_vip_config(会员配置表)】的数据库操作Service实现
* @createDate 2022-08-03 13:58:58
*/
@Service
public class VipConfigServiceImpl extends ServiceImpl<VipConfigMapper, VipConfigDO>
implements VipConfigService {

    @Override
    public PageBean<VipConfigVO> vipConfigPage(VipConfigDto dto) {

        LambdaQueryWrapper<VipConfigDO> wrapper = Wrappers.<VipConfigDO>lambdaQuery().like(StrUtil.isNotBlank(dto.getTypeName()), VipConfigDO::getTypeName, dto.getTypeName());
        Page<VipConfigDO> iPage = page(new Page<>(dto.getCurrent()-1, dto.getSize()),wrapper );
        PageBean<VipConfigVO> page = BeanCopyUtils.convertPage(iPage, VipConfigVO.class);
        return page;
    }

    @Override
    public R<Boolean> addCombo(VipConfigDto dto) {
        VipConfigDO configDO = BeanCopyUtils.convertBean(dto, VipConfigDO.class);
        // 计算实收金额
        configDO.setRealPrice(NumberUtil.sub(dto.getOriginalPrice(),dto.getDiscountAmount()));
        return R.ok(save(configDO));
    }

    @Override
    public R<Boolean> updateCombo(VipConfigDto dto) {
        VipConfigDO configDO = BeanCopyUtils.convertBean(dto, VipConfigDO.class);

        return R.ok( updateById(configDO));
    }

    @Override
    public R<VipConfigVO> getVipCombo(Long id) {
        VipConfigDO aDo = getById(id);
        if (ObjectUtil.isNull(aDo)){
            return R.ok(new VipConfigVO());
        }
        return R.ok(BeanCopyUtils.convertBean(aDo,VipConfigVO.class));
    }

    @Override
    public R<Boolean> delVipCombo(Long id) {
        return R.ok(this.removeById(id));
    }

    @Override
    public R<List<VipConfigVO>> getMiniVipConfigList() {
        List<VipConfigDO> doList = this.list(Wrappers.<VipConfigDO>lambdaQuery().eq(VipConfigDO::getState, VipConfigEnum.ON_THE_SHELF.getCode())
        );
        List<VipConfigVO> voList = BeanCopyUtils.convertList(doList, VipConfigVO.class);
        return R.ok(voList);
    }
}
